﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using System.Text;
using T.FAS.Runtime.Metadata.Entity;
using T.ServiceComponent.DAC.Service;
using T.ServiceComponent.DataAccessEx.Core;

namespace T.FAS.Runtime.Metadata.Dao
{
    /// <summary>
    /// 元数据依赖关系持久化类
    /// </summary>
    public class MetaDataDependencyDao : DaoService<MetaDataDependency>
    {
        public MetaDataDependencyDao(string db = "TFAS") : base()
        {
            this.DBName = db;
        }

        public override string IDColumnName
        {
            get
            {
                return "ID";
            }
        }

        public override string TableName
        {
            get
            {
                return "Meta_Dependency";
            }
        }

        public override List<string> ExceptColumnName
        {
            get
            {
                return new List<string>()
                {
                    "DependencyMetadataCode",
                    "DependencyMetadataName",
                    "DependencyMetaTypeCode",
                    "DependencyMetaTypeName",
                    "DependencyMetaStatus",
                    "DependencyMetaVersion",
                    "DependencyMetaCheckOutUserName",
                    "DependencyMetaCheckOutState",
                    "children",
                    "DependencyMetaTypeID"
                };
            }
        }

        public override Dictionary<string, object> GetDataMapping(MetaDataDependency data)
        {
            var datas = base.GetDataMapping(data);
            datas.Remove("DependencyType");
            datas.Add("DependencyType", (int)(data.DependencyType));

            datas.Remove("DeleteMark");
            datas.Add("DeleteMark", (bool)data.DeleteMark);
            return datas;
        }

        public override MetaDataDependency ConvertToObject(Dictionary<string, PropertyInfo> columns, DataRow row)
        {
            var myCol = new Dictionary<string, PropertyInfo>(base.Columns);
            myCol.Remove("DependencyType");
            myCol.Remove("DeleteMark");
            var data = base.ConvertToObject(myCol, row);

            if (row["DependencyType"] != null)
                data.DependencyType = (MetaDataDependencyType)Enum.Parse(typeof(MetaDataDependencyType),
                                                Convert.ToString(row["DependencyType"]));
            if (row["DeleteMark"] != DBNull.Value)
                data.DeleteMark = int.Parse(Convert.ToString(row["DeleteMark"])) == 0 ? false : true;

            return data;
        }

        public List<MetaDataDependency> GetAllDependencies(string projectBranchId, bool isNotGetDelDepens = false) {
            var dependMetadatas = new List<MetaDataDependency>();
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            string sqlCondition = "ProjectBranchID=@ProjectBranchID";
            if (isNotGetDelDepens)
                sqlCondition += " and DeleteMark=0 ";
            paramsList.Add(this.DB.MakeInParam("@ProjectBranchID", TDbDataType.VarChar, projectBranchId));
            var sql = string.Format(@"select ID, MetaDataID, ParentMetaDataID, DependencyType, ProjectBranchID, UpdateIsCacheClear, UpdateIsNeedReDeserialize, TenantID, Creator, CreateTime, LastModifier, LastModifyTime, DeleteMark,CheckOutUserID,QuoteProjectBranchID,QuoteMetaDataID FROM Meta_Dependency");
            var ds = DB.ExecuteDataSet(string.Format(@"{0} WHERE {1}", sql, sqlCondition), paramsList.ToArray());
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {                    
                    var dep = this.ConvertToObject(this.GetColumns(), row);                    
                    dependMetadatas.Add(dep);
                }
            }
            return dependMetadatas;
        }

        public void AddDependencies(List<MetaDataDependency> metaDataDependencies)
        {
            foreach (MetaDataDependency dep in metaDataDependencies)
            {
                List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
                paramsList.Add(this.DB.MakeInParam("@MetaDataID", TDbDataType.VarChar, dep.MetadataID));
                paramsList.Add(this.DB.MakeInParam("@ParentMetadataID", TDbDataType.VarChar, dep.ParentMetadataID));
                paramsList.Add(this.DB.MakeInParam("@ProjectBranchID", TDbDataType.VarChar, dep.ProjectBranchID));
                paramsList.Add(this.DB.MakeInParam("@CheckOutUserID", TDbDataType.VarChar, dep.CheckOutUserID));
                this.DB.ExecSqlStatement("Delete from Meta_Dependency WHERE MetaDataID=@MetaDataID and ParentMetadataID=@ParentMetadataID and ProjectBranchID=@ProjectBranchID AND CheckOutUserID=@CheckOutUserID", paramsList.ToArray());                
            }
            this.Add(metaDataDependencies);
        }

        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="metadataId"></param>
        /// <param name="projectBranchId"></param>
        /// <param name="IsMetaDataDelete"></param>
        public void DeleteMetaDataDependency(string metadataId, string projectBranchId, bool IsMetaDataDelete, string userID)
        {
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            paramsList.Add(this.DB.MakeInParam("@MetaDataID", TDbDataType.VarChar, metadataId));
            paramsList.Add(this.DB.MakeInParam("@ProjectBranchID", TDbDataType.VarChar, projectBranchId));
            string sqlCondition = "MetaDataID=@MetaDataID and ProjectBranchID=@ProjectBranchID";
            if (IsMetaDataDelete) {
                sqlCondition = "(MetaDataID=@MetaDataID Or ParentMetaDataID=@MetaDataID) and ProjectBranchID=@ProjectBranchID";
            }
            if (string.IsNullOrEmpty(userID) == false) {
                sqlCondition = string.Format("{0} AND CheckOutUserID=@CheckOutUserID", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@CheckOutUserID", TDbDataType.VarChar, userID));

            }
            this.DB.ExecSqlStatement(string.Format(@"update {0} set deleteMark = 1 WHERE {1}", TableName, sqlCondition), paramsList.ToArray());
        }

        public void DeleteAndUpdateMetaDataDependency(string metadataId, string projectBranchId, string userID)
        {
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            paramsList.Add(this.DB.MakeInParam("@MetaDataID", TDbDataType.VarChar, metadataId));
            paramsList.Add(this.DB.MakeInParam("@ProjectBranchID", TDbDataType.VarChar, projectBranchId));
            paramsList.Add(this.DB.MakeInParam("@CheckOutUserID", TDbDataType.VarChar, userID));
            this.DB.ExecSqlStatement("Delete from Meta_Dependency WHERE MetaDataID=@MetaDataID and ProjectBranchID=@ProjectBranchID AND (CheckOutUserID is null or CheckOutUserID='')", paramsList.ToArray());
            this.DB.ExecSqlStatement("Update Meta_Dependency set CheckOutUserID='' where MetaDataID=@MetaDataID and ProjectBranchID=@ProjectBranchID AND DeleteMark=0 AND CheckOutUserID=@CheckOutUserID", paramsList.ToArray());
        }
        public List<MetaDataDependency> GetCheckInDependMetadatas(string metadataId, string projectBranchId, string metadataVersionId, MetaDataDependencyType dependType = MetaDataDependencyType.None)
        {
            var dependMetadatas = new List<MetaDataDependency>();
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            // 2022-12-18 解决性能问题 去掉Code的join和where，去掉 or D.DeleteMark is null   Or meta.Code=@MetaDataID    Or Meta_BasicInfo.Code=D.ParentMetadataID Or meta.Code=D.MetaDataID
            string sqlCondition = "(meta.ID=@MetaDataID) and (D.DeleteMark != 1)";
            paramsList.Add(this.DB.MakeInParam("@MetaDataID", TDbDataType.VarChar, metadataId));
            if (string.IsNullOrEmpty(projectBranchId) == false)
            {
                sqlCondition = string.Format("{0} AND D.ProjectBranchID=@ProjectBranchID", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@ProjectBranchID", TDbDataType.VarChar, projectBranchId));
            }
            if (dependType != MetaDataDependencyType.None)
            {
                sqlCondition = string.Format("{0} AND D.DependencyType=@DependencyType", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@DependencyType", TDbDataType.Int, (int)dependType));
            }            
            sqlCondition = string.Format("{0} AND (D.CheckOutUserID is null Or D.CheckOutUserID='')", sqlCondition);
            if (string.IsNullOrEmpty(metadataVersionId) == false)
            {
                sqlCondition = string.Format("{0} AND D.ParentMetadataID in (select ParentMetaDataID from meta_dependencyversion mdv where mdv.MetadataVersionID=@MetadataVersionID)", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@MetadataVersionID", TDbDataType.VarChar, metadataVersionId));
            }
            var sql = string.Format(@"select D.ID,D.MetadataID,Meta_BasicInfo.Code as DependencyMetadataCode, Meta_BasicInfo.ID as ParentMetadataID,D.DependencyType, D.ProjectBranchID, D.UpdateIsCacheClear,D.UpdateIsNeedReDeserialize,D.TenantID,D.Creator,D.CreateTime,D.LastModifier,D.LastModifyTime,
Meta_BasicInfo.Name as DependencyMetadataName, Meta_BasicInfo.MetadataStatus as DependencyMetaStatus,Meta_TypeInfo.ID as  DependencyMetaTypeID, Meta_TypeInfo.Code as DependencyMetaTypeCode, Meta_TypeInfo.Name as DependencyMetaTypeName, D.DeleteMark,D.CheckOutUserID,D.QuoteProjectBranchID,D.QuoteMetaDataID
from Meta_Dependency D LEFT JOIN Meta_BasicInfo on (Meta_BasicInfo.ID=D.ParentMetadataID) and (Meta_BasicInfo.ProjectBranchID = D.ProjectBranchID or Meta_BasicInfo.ProjectBranchID = D.QuoteProjectBranchID) LEFT JOIN meta_basicinfo meta on (meta.ID=D.MetaDataID) and meta.ProjectBranchID = D.ProjectBranchID LEFT JOIN Meta_TypeInfo on Meta_TypeInfo.ID = Meta_BasicInfo.MetaTypeID ");

            var ds = DB.ExecuteDataSet(string.Format(@"{0} WHERE {1} Order by D.MetaDataID,LastModifyTime DESC", sql, sqlCondition), paramsList.ToArray());
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    var row = ds.Tables[0].Rows[i];
                    var dep = this.ConvertToObject(this.GetColumns(), row);
                    if (dependMetadatas.FindIndex(depMeta => depMeta.MetadataID == dep.MetadataID && depMeta.ParentMetadataID == dep.ParentMetadataID) > -1)
                    {
                        continue;
                    }
                    dep.DependencyMetadataName = Convert.ToString(row["DependencyMetadataName"]);
                    dep.DependencyMetadataCode = Convert.ToString(row["DependencyMetadataCode"]);
                    dep.DependencyMetaStatus = Convert.ToString(row["DependencyMetaStatus"]);
                    dep.DependencyMetaTypeCode = Convert.ToString(row["DependencyMetaTypeCode"]);
                    dep.DependencyMetaTypeName = Convert.ToString(row["DependencyMetaTypeName"]);
                    dep.DependencyMetaTypeID = Convert.ToString(row["DependencyMetaTypeID"]);
                    dependMetadatas.Add(dep);
                }
            }

            return dependMetadatas;
        }

        public List<MetaDataDependency> GetDependMetadatas(string metadataId, string projectBranchId, MetaDataDependencyType dependType = MetaDataDependencyType.None, string checkOutUserID = "")
        {
            var dependMetadatas = new List<MetaDataDependency>();
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            // 2022-12-18 解决性能问题 去掉Code的join和where，去掉 or D.DeleteMark is null   Or meta.Code=@MetaDataID    Or Meta_BasicInfo.Code=D.ParentMetadataID Or meta.Code=D.MetaDataID
            string sqlCondition = "(meta.ID=@MetaDataID) and (D.DeleteMark != 1)";
            paramsList.Add(this.DB.MakeInParam("@MetaDataID", TDbDataType.VarChar, metadataId));
            if (string.IsNullOrEmpty(projectBranchId) == false) {
                sqlCondition = string.Format("{0} AND (D.ProjectBranchID=@ProjectBranchID or D.ProjectBranchID='Public')", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@ProjectBranchID", TDbDataType.VarChar, projectBranchId));
            }
            if (dependType != MetaDataDependencyType.None) {
                sqlCondition = string.Format("{0} AND D.DependencyType=@DependencyType", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@DependencyType", TDbDataType.Int, (int)dependType));
            }
            if (string.IsNullOrEmpty(checkOutUserID) == false)
            {
                sqlCondition = string.Format("{0} AND D.CheckOutUserID=@CheckOutUserID", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@CheckOutUserID", TDbDataType.VarChar, checkOutUserID));
            }
            else {
                sqlCondition = string.Format("{0} AND (D.CheckOutUserID is null Or D.CheckOutUserID='' Or D.CheckOutUserID=@CheckOutUserID)", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@CheckOutUserID", TDbDataType.VarChar, UserUtil.GetCurrentUserID()));
            }
            var sql = string.Format(@"select D.ID,D.MetadataID,Meta_BasicInfo.Code as DependencyMetadataCode, Meta_BasicInfo.ID as ParentMetadataID,D.DependencyType, Meta_BasicInfo.ProjectBranchID, D.UpdateIsCacheClear,D.UpdateIsNeedReDeserialize,D.TenantID,D.Creator,D.CreateTime,D.LastModifier,D.LastModifyTime,
Meta_BasicInfo.Name as DependencyMetadataName, Meta_BasicInfo.MetadataStatus as DependencyMetaStatus,Meta_TypeInfo.ID as  DependencyMetaTypeID, Meta_TypeInfo.Code as DependencyMetaTypeCode, Meta_TypeInfo.Name as DependencyMetaTypeName, D.DeleteMark,D.CheckOutUserID,D.QuoteProjectBranchID,D.QuoteMetaDataID
from Meta_Dependency D LEFT JOIN Meta_BasicInfo on (Meta_BasicInfo.ID=D.ParentMetadataID) and  (Meta_BasicInfo.ProjectBranchID = D.ProjectBranchID  or Meta_BasicInfo.ProjectBranchID=D.QuoteProjectBranchID  or Meta_BasicInfo.ProjectBranchID='public') LEFT JOIN meta_basicinfo meta on (meta.ID=D.MetaDataID) and  (meta.ProjectBranchID = D.ProjectBranchID or meta.ProjectBranchID=D.QuoteProjectBranchID) LEFT JOIN Meta_TypeInfo on Meta_TypeInfo.ID = Meta_BasicInfo.MetaTypeID ");

            var ds = DB.ExecuteDataSet(string.Format(@"{0} WHERE {1} Order by D.MetaDataID,LastModifyTime DESC", sql, sqlCondition), paramsList.ToArray());
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    var row = ds.Tables[0].Rows[i];
                    var dep = this.ConvertToObject(this.GetColumns(), row);
                    if (dependMetadatas.FindIndex(depMeta => depMeta.MetadataID == dep.MetadataID && depMeta.ParentMetadataID == dep.ParentMetadataID)>-1) {
                        continue;
                    }
                    dep.DependencyMetadataName = Convert.ToString(row["DependencyMetadataName"]);
                    dep.DependencyMetadataCode = Convert.ToString(row["DependencyMetadataCode"]);
                    dep.DependencyMetaStatus = Convert.ToString(row["DependencyMetaStatus"]);
                    dep.DependencyMetaTypeCode = Convert.ToString(row["DependencyMetaTypeCode"]);
                    dep.DependencyMetaTypeName = Convert.ToString(row["DependencyMetaTypeName"]);
                    dep.DependencyMetaTypeID = Convert.ToString(row["DependencyMetaTypeID"]);
                    dependMetadatas.Add(dep);
                }
            }

            return dependMetadatas;
        }        

        public List<MetaDataDependency> GetBeDependencyMetadatas(string metadataId, string projectBranchId, MetaDataDependencyType dependType = MetaDataDependencyType.None, string checkOutUserID = "")
        {
            var dependMetadatas = new List<MetaDataDependency>();
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();           
            // 2022-12-18 解决性能问题 去掉Code的join和where
            string sqlCondition = "(meta.ID=@MetaDataID) and (D.DeleteMark != 1) ";
            paramsList.Add(this.DB.MakeInParam("@MetaDataID", TDbDataType.VarChar, metadataId));
            if (string.IsNullOrEmpty(projectBranchId) == false)
            {
                sqlCondition = string.Format("{0} AND D.ProjectBranchID=@ProjectBranchID", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@ProjectBranchID", TDbDataType.VarChar, projectBranchId));
            }
            if (dependType != MetaDataDependencyType.None)
            {
                sqlCondition = string.Format("{0} AND D.DependencyType=@DependencyType", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@DependencyType", TDbDataType.Int, (int)dependType));
            }
            if (string.IsNullOrEmpty(checkOutUserID) == false)
            {
                sqlCondition = string.Format("{0} AND D.CheckOutUserID=@CheckOutUserID", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@CheckOutUserID", TDbDataType.VarChar, checkOutUserID));
            }
            var sql = string.Format(@"select D.ID,Meta_BasicInfo.ID as MetadataID,D.ParentMetadataID,D.DependencyType,D.ProjectBranchID, D.UpdateIsCacheClear,D.UpdateIsNeedReDeserialize,D.TenantID,D.Creator,D.CreateTime,D.LastModifier,D.LastModifyTime,
Meta_BasicInfo.Code as DependencyMetadataCode,Meta_BasicInfo.Name as DependencyMetadataName, Meta_TypeInfo.Code as DependencyMetaTypeCode, Meta_TypeInfo.Name as DependencyMetaTypeName, D.DeleteMark,D.CheckOutUserID,D.QuoteProjectBranchID,D.QuoteMetaDataID
from Meta_Dependency D LEFT JOIN Meta_BasicInfo on (Meta_BasicInfo.ID=D.MetadataID) and (Meta_BasicInfo.ProjectBranchID = D.ProjectBranchID ) LEFT JOIN meta_basicinfo meta on (meta.ID=D.ParentMetadataID) and(meta.ID=D.ParentMetadataID) and (meta.ProjectBranchID = D.ProjectBranchID ) LEFT JOIN Meta_TypeInfo on Meta_TypeInfo.ID = Meta_BasicInfo.MetaTypeID ");

            var ds = DB.ExecuteDataSet(string.Format(@"{0} WHERE {1}", sql, sqlCondition), paramsList.ToArray());
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    var row = ds.Tables[0].Rows[i];
                    var dep = this.ConvertToObject(this.GetColumns(), row);
                    if (dependMetadatas.FindIndex(depMeta => depMeta.MetadataID == dep.MetadataID && depMeta.ParentMetadataID == dep.ParentMetadataID) > -1)
                    {
                        continue;
                    }
                    dep.DependencyMetadataName = Convert.ToString(row["DependencyMetadataName"]);
                    dep.DependencyMetadataCode = Convert.ToString(row["DependencyMetadataCode"]);
                    dep.DependencyMetaTypeCode = Convert.ToString(row["DependencyMetaTypeCode"]);
                    dep.DependencyMetaTypeName = Convert.ToString(row["DependencyMetaTypeName"]);
                    dependMetadatas.Add(dep);
                }
            }

            return dependMetadatas;
        }
        /// <summary>
        /// 获取所有依赖元数据
        /// </summary>
        /// <param name="metadataId"></param>
        /// <param name="projectBranchId"></param>
        /// <param name="dependType"></param>
        /// <returns></returns>
        public List<MetaDataDependency> GetAllDependMetadatas(string metadataId, string projectBranchId, MetaDataDependencyType dependType = MetaDataDependencyType.None, string checkOutUserID="")
        {
            var dependMetadatas = new List<MetaDataDependency>();
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            // 2022-12-18 解决性能问题 去掉Code的join和where
            string sqlCondition = "(meta.ID=@MetaDataID)";
            paramsList.Add(this.DB.MakeInParam("@MetaDataID", TDbDataType.VarChar, metadataId));
            if (string.IsNullOrEmpty(projectBranchId) == false)
            {
                sqlCondition = string.Format("{0} AND D.ProjectBranchID=@ProjectBranchID", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@ProjectBranchID", TDbDataType.VarChar, projectBranchId));
            }
            if (dependType != MetaDataDependencyType.None)
            {
                sqlCondition = string.Format("{0} AND D.DependencyType=@DependencyType", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@DependencyType", TDbDataType.Int, (int)dependType));
            }
            if (string.IsNullOrEmpty(checkOutUserID) == false) {
                sqlCondition = string.Format("{0} AND D.CheckOutUserID=@CheckOutUserID", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@CheckOutUserID", TDbDataType.VarChar, checkOutUserID));
            }
            var sql = string.Format(@"select D.ID,D.MetadataID,Meta_BasicInfo.Code as DependencyMetadataCode, Meta_BasicInfo.ID as ParentMetadataID,D.DependencyType, D.ProjectBranchID, D.UpdateIsCacheClear,D.UpdateIsNeedReDeserialize,D.TenantID,D.Creator,D.CreateTime,D.LastModifier,D.LastModifyTime,
Meta_BasicInfo.Name as DependencyMetadataName, Meta_BasicInfo.MetaTypeID as DependencyMetaTypeCode, D.DeleteMark,D.CheckOutUserID,D.QuoteProjectBranchID,D.QuoteMetaDataID
from Meta_Dependency D LEFT JOIN Meta_BasicInfo on (Meta_BasicInfo.ID=D.ParentMetadataID) and Meta_BasicInfo.ProjectBranchID = D.ProjectBranchID LEFT JOIN meta_basicinfo meta on (meta.ID=D.MetaDataID) and meta.ProjectBranchID = D.ProjectBranchID ");

            var ds = DB.ExecuteDataSet(string.Format(@"{0} WHERE {1}", sql, sqlCondition), paramsList.ToArray());
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    var row = ds.Tables[0].Rows[i];
                    var dep = this.ConvertToObject(this.GetColumns(), row);
                    dep.DependencyMetadataName = Convert.ToString(row["DependencyMetadataName"]);
                    dep.DependencyMetadataCode = Convert.ToString(row["DependencyMetadataCode"]);
                    dep.DependencyMetaTypeCode = Convert.ToString(row["DependencyMetaTypeCode"]);
                    dependMetadatas.Add(dep);
                }
            }

            return dependMetadatas;
        }
        /// <summary>
        /// 获取所有被依赖元数据
        /// </summary>
        /// <param name="metadataId"></param>
        /// <param name="projectBranchId"></param>
        /// <param name="dependType"></param>
        /// <returns></returns>
        public List<MetaDataDependency> GetAllBeDependencyMetadatas(string metadataId, string projectBranchId, MetaDataDependencyType dependType = MetaDataDependencyType.None, string checkOutUserID = "")
        {
            var dependMetadatas = new List<MetaDataDependency>();
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            // 2022-12-18 解决性能问题 去掉Code的join和where
            string sqlCondition = "(meta.ID=@MetaDataID)";
            paramsList.Add(this.DB.MakeInParam("@MetaDataID", TDbDataType.VarChar, metadataId));
            if (string.IsNullOrEmpty(projectBranchId) == false)
            {
                sqlCondition = string.Format("{0} AND D.ProjectBranchID=@ProjectBranchID", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@ProjectBranchID", TDbDataType.VarChar, projectBranchId));
            }
            if (dependType != MetaDataDependencyType.None)
            {
                sqlCondition = string.Format("{0} AND D.DependencyType=@DependencyType", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@DependencyType", TDbDataType.Int, (int)dependType));
            }
            if (string.IsNullOrEmpty(checkOutUserID) == false)
            {
                if (checkOutUserID == "other") checkOutUserID = "";
                if (checkOutUserID == "otheruser") {
                    checkOutUserID = UserUtil.GetCurrentUserID();
                    sqlCondition = string.Format("{0} AND (D.CheckOutUserID!=@CheckOutUserID AND D.CheckOutUserID !='' AND DeleteMark=0)", sqlCondition);
                }
                else { 
                    sqlCondition = string.Format("{0} AND D.CheckOutUserID=@CheckOutUserID", sqlCondition);
                }
                paramsList.Add(this.DB.MakeInParam("@CheckOutUserID", TDbDataType.VarChar, checkOutUserID));
            }
            var sql = string.Format(@"select D.ID,Meta_BasicInfo.ID as MetadataID,D.ParentMetadataID,D.DependencyType,D.ProjectBranchID, D.UpdateIsCacheClear,D.UpdateIsNeedReDeserialize,D.TenantID,D.Creator,D.CreateTime,D.LastModifier,D.LastModifyTime,
Meta_BasicInfo.Code as DependencyMetadataCode,Meta_BasicInfo.Name as DependencyMetadataName,Meta_BasicInfo.MetaTypeID as DependencyMetaTypeCode, D.DeleteMark,D.CheckOutUserID,D.QuoteProjectBranchID,D.QuoteMetaDataID
from Meta_Dependency D LEFT JOIN Meta_BasicInfo on (Meta_BasicInfo.ID=D.MetadataID) and Meta_BasicInfo.ProjectBranchID = D.ProjectBranchID LEFT JOIN meta_basicinfo meta on (meta.ID=D.ParentMetadataID) and (meta.ProjectBranchID = D.ProjectBranchID or meta.ProjectBranchID = D.QuoteProjectBranchID)");

            var ds = DB.ExecuteDataSet(string.Format(@"{0} WHERE {1}", sql, sqlCondition), paramsList.ToArray());
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    var row = ds.Tables[0].Rows[i];
                    var dep = this.ConvertToObject(this.GetColumns(), row);
                    dep.DependencyMetadataName = Convert.ToString(row["DependencyMetadataName"]);
                    dep.DependencyMetadataCode = Convert.ToString(row["DependencyMetadataCode"]);
                    dep.DependencyMetaTypeCode = Convert.ToString(row["DependencyMetaTypeCode"]);
                    dependMetadatas.Add(dep);
                }
            }

            return dependMetadatas;
        }

        /// <summary>
        /// 获取依赖元数据详细信息
        /// </summary>
        /// <param name="metadataId"></param>
        /// <param name="projectBranchId"></param>
        /// <param name="dependType"></param>
        /// <returns></returns>
        public List<MetaDataDependency> GetDependMetadataDetails(string metadataId, string projectBranchId, MetaDataDependencyType dependType)
        {
            var dependMetadatas = new List<MetaDataDependency>();
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();            
            string sqlCondition = "(D.MetaDataID=@MetaDataID) and (D.DeleteMark != 1)";
            paramsList.Add(this.DB.MakeInParam("@MetaDataID", TDbDataType.VarChar, metadataId));
            if (string.IsNullOrEmpty(projectBranchId) == false)
            {
                sqlCondition = string.Format("{0} AND D.ProjectBranchID=@ProjectBranchID", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@ProjectBranchID", TDbDataType.VarChar, projectBranchId));
            }
            if (dependType != MetaDataDependencyType.None)
            {
                sqlCondition = string.Format("{0} AND D.DependencyType=@DependencyType", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@DependencyType", TDbDataType.Int, (int)dependType));
            }
            
            sqlCondition = string.Format("{0} AND (D.CheckOutUserID is null Or D.CheckOutUserID='' Or D.CheckOutUserID=@CheckOutUserID)", sqlCondition);
            paramsList.Add(this.DB.MakeInParam("@CheckOutUserID", TDbDataType.VarChar, UserUtil.GetCurrentUserID()));
           
            var sql = string.Format(@"select D.ID,D.MetadataID,D.DependencyType, D.ProjectBranchID, D.UpdateIsCacheClear,D.UpdateIsNeedReDeserialize,D.TenantID,D.Creator,D.CreateTime,D.LastModifier,D.LastModifyTime, D.DeleteMark,D.CheckOutUserID,D.QuoteProjectBranchID,D.QuoteMetaDataID,Meta_BasicInfo.Code as DependencyMetadataCode, Meta_BasicInfo.ID as ParentMetadataID,Meta_BasicInfo.Name as DependencyMetadataName, Meta_BasicInfo.MetadataStatus as DependencyMetaStatus, Meta_TypeInfo.Code as DependencyMetaTypeCode, Meta_TypeInfo.Name as DependencyMetaTypeName, mv.Version as DependencyMetaVersion, checkout.CheckOutUserName as DependencyMetaCheckOutUserName, checkout.CheckOutState as DependencyMetaCheckOutState from Meta_Dependency D 
LEFT JOIN Meta_BasicInfo on Meta_BasicInfo.ID=D.ParentMetadataID and (Meta_BasicInfo.ProjectBranchID = D.ProjectBranchID Or Meta_BasicInfo.ProjectBranchID='Public' or Meta_BasicInfo.ProjectBranchID=D.QuoteProjectBranchID)
LEFT JOIN Meta_Version mv on mv.ID = Meta_BasicInfo.CurrentVersionID and (mv.ProjectBranchID = D.ProjectBranchID or mv.ProjectBranchID=D.QuoteProjectBranchID)
LEFT JOIN Meta_TypeInfo on Meta_TypeInfo.ID = Meta_BasicInfo.MetaTypeID 
LEFT JOIN meta_checkout checkout on checkout.MetadataID=D.ParentMetadataID and checkout.ProjectBranchID = D.ProjectBranchID AND (CheckOutState IN (0, 1, 5) AND checkout.CheckOutUserID = @CheckOutUserID)");

            var ds = DB.ExecuteDataSet(string.Format(@"{0} WHERE {1} Order By D.CheckOutUserID DESC,D.LastModifyTime DESC", sql, sqlCondition), paramsList.ToArray());
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    var row = ds.Tables[0].Rows[i];
                    var dep = this.ConvertToObject(this.GetColumns(), row);
                    if (dependMetadatas.FindIndex(depMeta => depMeta.MetadataID == dep.MetadataID && depMeta.ParentMetadataID == dep.ParentMetadataID) > -1)
                    {
                        continue;
                    }
                    dep.DependencyMetadataCode = Convert.ToString(row["DependencyMetadataCode"]);
                    if (string.IsNullOrEmpty(dep.ParentMetadataID)) continue;
                    dep.DependencyMetadataName = Convert.ToString(row["DependencyMetadataName"]);
                    dep.DependencyMetaStatus = Convert.ToString(row["DependencyMetaStatus"]);
                    dep.DependencyMetaTypeCode = Convert.ToString(row["DependencyMetaTypeCode"]);
                    dep.DependencyMetaTypeName = Convert.ToString(row["DependencyMetaTypeName"]);
                    dep.DependencyMetaVersion = Convert.ToString(row["DependencyMetaVersion"]);
                    dep.DependencyMetaCheckOutUserName = Convert.ToString(row["DependencyMetaCheckOutUserName"]);
                    dep.DependencyMetaCheckOutState = Convert.ToString(row["DependencyMetaCheckOutState"]);
                    dependMetadatas.Add(dep);
                }
            }

            return dependMetadatas;
        }
        /// <summary>
        /// 获取被依赖元数据详细信息
        /// </summary>
        /// <param name="metadataId"></param>
        /// <param name="projectBranchId"></param>
        /// <param name="dependType"></param>
        /// <returns></returns>
        public List<MetaDataDependency> GetBeDependencyMetadataDetails(string metadataId, string projectBranchId, MetaDataDependencyType dependType)
        {
            var dependMetadatas = new List<MetaDataDependency>();
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            // 2022-12-18 解决性能问题 去掉Code的join和where
            string sqlCondition = "(D.ParentMetadataID=@MetaDataID) and (D.DeleteMark != 1) ";
            paramsList.Add(this.DB.MakeInParam("@MetaDataID", TDbDataType.VarChar, metadataId));
            if (string.IsNullOrEmpty(projectBranchId) == false)
            {
                sqlCondition = string.Format("{0} AND D.ProjectBranchID=@ProjectBranchID", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@ProjectBranchID", TDbDataType.VarChar, projectBranchId));
            }
            if (dependType != MetaDataDependencyType.None)
            {
                sqlCondition = string.Format("{0} AND D.DependencyType=@DependencyType", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@DependencyType", TDbDataType.Int, (int)dependType));
            }
            sqlCondition = string.Format("{0} AND (D.CheckOutUserID is null Or D.CheckOutUserID='' Or D.CheckOutUserID=@CheckOutUserID)", sqlCondition);
            paramsList.Add(this.DB.MakeInParam("@CheckOutUserID", TDbDataType.VarChar, UserUtil.GetCurrentUserID()));
            var sql = string.Format(@"select D.ID,D.ParentMetadataID,D.DependencyType,D.ProjectBranchID,D.UpdateIsCacheClear,D.UpdateIsNeedReDeserialize,D.TenantID,D.Creator,D.CreateTime,D.LastModifier,D.LastModifyTime, D.DeleteMark,D.CheckOutUserID,D.QuoteProjectBranchID,D.QuoteMetaDataID,Meta_BasicInfo.ID as MetadataID, Meta_BasicInfo.Code as DependencyMetadataCode,Meta_BasicInfo.Name as DependencyMetadataName, Meta_BasicInfo.MetadataStatus as DependencyMetaStatus, Meta_TypeInfo.Code as DependencyMetaTypeCode, Meta_TypeInfo.Name as DependencyMetaTypeName, mv.Version as DependencyMetaVersion, checkout.CheckOutUserName as DependencyMetaCheckOutUserName, checkout.CheckOutState as DependencyMetaCheckOutState from Meta_Dependency D 
LEFT JOIN Meta_BasicInfo on (Meta_BasicInfo.ID=D.MetadataID) and Meta_BasicInfo.ProjectBranchID = D.ProjectBranchID 
LEFT JOIN Meta_Version mv on mv.ID = Meta_BasicInfo.CurrentVersionID and mv.ProjectBranchID = D.ProjectBranchID
LEFT JOIN Meta_TypeInfo on Meta_TypeInfo.ID = Meta_BasicInfo.MetaTypeID
LEFT JOIN meta_checkout checkout on checkout.MetadataID=D.MetadataID and checkout.ProjectBranchID = D.ProjectBranchID  and (CheckOutState IN(0, 1, 5) AND checkout.CheckOutUserID = @CheckOutUserID) ");

            var ds = DB.ExecuteDataSet(string.Format(@"{0} WHERE {1} Order By D.CheckOutUserID DESC,D.LastModifyTime DESC", sql, sqlCondition), paramsList.ToArray());
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    var row = ds.Tables[0].Rows[i];
                    var dep = this.ConvertToObject(this.GetColumns(), row);
                    if (dependMetadatas.FindIndex(depMeta => depMeta.MetadataID == dep.MetadataID && depMeta.ParentMetadataID == dep.ParentMetadataID) > -1)
                    {
                        continue;
                    }
                    dep.DependencyMetadataName = Convert.ToString(row["DependencyMetadataName"]);
                    dep.DependencyMetadataCode = Convert.ToString(row["DependencyMetadataCode"]);
                    dep.DependencyMetaStatus = Convert.ToString(row["DependencyMetaStatus"]);
                    dep.DependencyMetaTypeCode = Convert.ToString(row["DependencyMetaTypeCode"]);
                    dep.DependencyMetaTypeName = Convert.ToString(row["DependencyMetaTypeName"]);
                    dep.DependencyMetaVersion = Convert.ToString(row["DependencyMetaVersion"]);
                    dep.DependencyMetaCheckOutUserName = Convert.ToString(row["DependencyMetaCheckOutUserName"]);
                    dep.DependencyMetaCheckOutState = Convert.ToString(row["DependencyMetaCheckOutState"]);
                    dependMetadatas.Add(dep);
                }
            }

            return dependMetadatas;
        }

        #region 含引用依赖
        public List<MetaDataDependency> GetDependMetadatasWithQuote(string metadataId, string projectBranchId, MetaDataDependencyType dependType = MetaDataDependencyType.None, string checkOutUserID = "")
        {
            var dependMetadatas = new List<MetaDataDependency>();
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            // 2022-12-18 解决性能问题 去掉Code的join和where，去掉 or D.DeleteMark is null   Or meta.Code=@MetaDataID    Or Meta_BasicInfo.Code=D.ParentMetadataID Or meta.Code=D.MetaDataID
            string sqlCondition = "(meta.ID=@MetaDataID) and (D.DeleteMark != 1)";
            paramsList.Add(this.DB.MakeInParam("@MetaDataID", TDbDataType.VarChar, metadataId));
            if (string.IsNullOrEmpty(projectBranchId) == false)
            {
                sqlCondition = string.Format("{0} AND D.ProjectBranchID=@ProjectBranchID", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@ProjectBranchID", TDbDataType.VarChar, projectBranchId));
            }
            if (dependType != MetaDataDependencyType.None)
            {
                sqlCondition = string.Format("{0} AND D.DependencyType=@DependencyType", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@DependencyType", TDbDataType.Int, (int)dependType));
            }
            if (string.IsNullOrEmpty(checkOutUserID) == false)
            {
                sqlCondition = string.Format("{0} AND D.CheckOutUserID=@CheckOutUserID", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@CheckOutUserID", TDbDataType.VarChar, checkOutUserID));
            }
            else
            {
                sqlCondition = string.Format("{0} AND (D.CheckOutUserID is null Or D.CheckOutUserID='' Or D.CheckOutUserID=@CheckOutUserID)", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@CheckOutUserID", TDbDataType.VarChar, UserUtil.GetCurrentUserID()));
            }
            var sql = string.Format(@"select D.ID,D.MetadataID,Meta_BasicInfo.Code as DependencyMetadataCode, Meta_BasicInfo.ID as ParentMetadataID,D.DependencyType, D.ProjectBranchID, D.UpdateIsCacheClear,D.UpdateIsNeedReDeserialize,D.TenantID,D.Creator,D.CreateTime,D.LastModifier,D.LastModifyTime,
Meta_BasicInfo.Name as DependencyMetadataName, Meta_BasicInfo.MetadataStatus as DependencyMetaStatus, Meta_TypeInfo.Code as DependencyMetaTypeCode, Meta_TypeInfo.Name as DependencyMetaTypeName, D.DeleteMark,D.CheckOutUserID,D.QuoteProjectBranchID,D.QuoteMetaDataID
from Meta_Dependency D LEFT JOIN Meta_BasicInfo on (Meta_BasicInfo.ID=D.ParentMetadataID) and (Meta_BasicInfo.ProjectBranchID = D.ProjectBranchID or Meta_BasicInfo.ProjectBranchID = D.QuoteProjectBranchID) LEFT JOIN meta_basicinfo meta on (meta.ID=D.MetaDataID) and meta.ProjectBranchID = D.ProjectBranchID LEFT JOIN Meta_TypeInfo on Meta_TypeInfo.ID = Meta_BasicInfo.MetaTypeID ");

            var ds = DB.ExecuteDataSet(string.Format(@"{0} WHERE {1} Order by D.MetaDataID,LastModifyTime DESC", sql, sqlCondition), paramsList.ToArray());
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    var row = ds.Tables[0].Rows[i];
                    var dep = this.ConvertToObject(this.GetColumns(), row);
                    if (dependMetadatas.FindIndex(depMeta => depMeta.MetadataID == dep.MetadataID && depMeta.ParentMetadataID == dep.ParentMetadataID) > -1)
                    {
                        continue;
                    }
                    dep.DependencyMetadataName = Convert.ToString(row["DependencyMetadataName"]);
                    dep.DependencyMetadataCode = Convert.ToString(row["DependencyMetadataCode"]);
                    dep.DependencyMetaStatus = Convert.ToString(row["DependencyMetaStatus"]);
                    dep.DependencyMetaTypeCode = Convert.ToString(row["DependencyMetaTypeCode"]);
                    dep.DependencyMetaTypeName = Convert.ToString(row["DependencyMetaTypeName"]);
                    dependMetadatas.Add(dep);
                }
            }

            return dependMetadatas;
        }

        public List<MetaDataDependency> GetBeDependencyMetadatasWithQuote(string metadataId, string projectBranchId, MetaDataDependencyType dependType = MetaDataDependencyType.None, string checkOutUserID = "")
        {
            var dependMetadatas = new List<MetaDataDependency>();
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            // 2022-12-18 解决性能问题 去掉Code的join和where
            string sqlCondition = "(meta.ID=@MetaDataID) and (D.DeleteMark != 1) ";
            paramsList.Add(this.DB.MakeInParam("@MetaDataID", TDbDataType.VarChar, metadataId));
            if (string.IsNullOrEmpty(projectBranchId) == false)
            {
                sqlCondition = string.Format("{0} AND (D.ProjectBranchID=@ProjectBranchID Or D.QuoteProjectBranchID=@ProjectBranchID)", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@ProjectBranchID", TDbDataType.VarChar, projectBranchId));
            }
            if (dependType != MetaDataDependencyType.None)
            {
                sqlCondition = string.Format("{0} AND D.DependencyType=@DependencyType", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@DependencyType", TDbDataType.Int, (int)dependType));
            }
            if (string.IsNullOrEmpty(checkOutUserID) == false)
            {
                sqlCondition = string.Format("{0} AND D.CheckOutUserID=@CheckOutUserID", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@CheckOutUserID", TDbDataType.VarChar, checkOutUserID));
            }
            var sql = string.Format(@"select D.ID,Meta_BasicInfo.ID as MetadataID,D.ParentMetadataID,D.DependencyType,D.ProjectBranchID, D.UpdateIsCacheClear,D.UpdateIsNeedReDeserialize,D.TenantID,D.Creator,D.CreateTime,D.LastModifier,D.LastModifyTime,
Meta_BasicInfo.Code as DependencyMetadataCode,Meta_BasicInfo.Name as DependencyMetadataName, Meta_TypeInfo.Code as DependencyMetaTypeCode, Meta_TypeInfo.Name as DependencyMetaTypeName, D.DeleteMark,D.CheckOutUserID,D.QuoteProjectBranchID,D.QuoteMetaDataID
from Meta_Dependency D LEFT JOIN Meta_BasicInfo on (Meta_BasicInfo.ID=D.MetadataID) and (Meta_BasicInfo.ProjectBranchID = D.ProjectBranchID or Meta_BasicInfo.ProjectBranchID = D.QuoteProjectBranchID) LEFT JOIN meta_basicinfo meta on (meta.ID=D.ParentMetadataID) and(meta.ID=D.ParentMetadataID) and (meta.ProjectBranchID = D.ProjectBranchID or meta.ProjectBranchID = D.QuoteProjectBranchID) LEFT JOIN Meta_TypeInfo on Meta_TypeInfo.ID = Meta_BasicInfo.MetaTypeID ");

            var ds = DB.ExecuteDataSet(string.Format(@"{0} WHERE {1}", sql, sqlCondition), paramsList.ToArray());
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    var row = ds.Tables[0].Rows[i];
                    var dep = this.ConvertToObject(this.GetColumns(), row);
                    dep.DependencyMetadataName = Convert.ToString(row["DependencyMetadataName"]);
                    dep.DependencyMetadataCode = Convert.ToString(row["DependencyMetadataCode"]);
                    dep.DependencyMetaTypeCode = Convert.ToString(row["DependencyMetaTypeCode"]);
                    dep.DependencyMetaTypeName = Convert.ToString(row["DependencyMetaTypeName"]);
                    dependMetadatas.Add(dep);
                }
            }

            return dependMetadatas;
        }

        public List<MetaDataDependency> GetDependencyMetasNotQuote(string metadataId, string projectBranchId, string quoteProjectBranchId) {
            var dependMetadatas = new List<MetaDataDependency>();
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>() {
                this.DB.MakeInParam("@ProjectBranchID", TDbDataType.VarChar, projectBranchId),
                this.DB.MakeInParam("@QuoteProjectBranchID", TDbDataType.VarChar, quoteProjectBranchId),
                this.DB.MakeInParam("@QuoteMetaDataID", TDbDataType.VarChar, metadataId)
            };
            string sql = @"select ID, MetaDataID, ParentMetaDataID, DependencyType, ProjectBranchID, UpdateIsCacheClear, UpdateIsNeedReDeserialize, TenantID, Creator, CreateTime, LastModifier, LastModifyTime, DeleteMark,CheckOutUserID,QuoteProjectBranchID,QuoteMetaDataID from meta_dependency where ProjectBranchID=@QuoteProjectBranchID and MetaDataID=@QuoteMetaDataID and DeleteMark=0 and (CheckOutUserID='' or CheckOutUserID is null) and ParentMetaDataID not in (select QuoteMetaDataID from fas_projecttree where ProjectBranchID = @ProjectBranchID and QuoteMetaDataID is not null)";
            var ds = DB.ExecuteDataSet(sql, paramsList.ToArray());
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    var row = ds.Tables[0].Rows[i];
                    var dep = this.ConvertToObject(this.GetColumns(), row);
                    if (dependMetadatas.FindIndex(depMeta => depMeta.MetadataID == dep.MetadataID && depMeta.ParentMetadataID == dep.ParentMetadataID) > -1)
                    {
                        continue;
                    }
                    dependMetadatas.Add(dep);
                }
            }

            return dependMetadatas;
        }
        #endregion

        public List<Tuple<string, string, string>> GetDependencyMetadataVersions(string metadataId, string projectBranchId) {
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            // 2022-12-18 解决性能问题 去掉Code的join和where
            string sqlCondition = "dep.MetadataID=@MetaDataID and dep.DeleteMark != 1 AND (dep.CheckOutUserID is null Or dep.CheckOutUserID='') ";
            paramsList.Add(this.DB.MakeInParam("@MetaDataID", TDbDataType.VarChar, metadataId));
            if (string.IsNullOrEmpty(projectBranchId) == false)
            {
                sqlCondition = string.Format("{0} AND dep.ProjectBranchID=@ProjectBranchID", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@ProjectBranchID", TDbDataType.VarChar, projectBranchId));
            }
            List<Tuple<string, string, string>> tuples = new List<Tuple<string, string, string>>();
            var ds = DB.ExecuteDataSet(string.Format(@"select dep.ParentMetaDataID, meta.CurrentVersionID,mv.Version from meta_dependency dep
left join meta_basicinfo meta on dep.ParentMetaDataID = meta.ID and (dep.ProjectBranchID = meta.ProjectBranchID or dep.QuoteProjectBranchID = meta.ProjectBranchID)
left join meta_version mv on meta.CurrentVersionID=mv.ID WHERE {0}", sqlCondition), paramsList.ToArray());
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows) {
                    if(DBNull.Value == row["ParentMetaDataID"] || DBNull.Value == row["CurrentVersionID"])
                    {
                        continue;
                    }
                    string ParentMetaDataID = Convert.ToString(row["ParentMetaDataID"]);
                    string CurrentVersionID = Convert.ToString(row["CurrentVersionID"]);
                    string Version = Convert.ToString(row["Version"]);
                    tuples.Add(new Tuple<string, string, string>(ParentMetaDataID, CurrentVersionID, Version));
                }
            }
            return tuples;
        }
    }
}
